//
//  HomeItemView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/2/3.
//  ai日记首页
//

import SwiftUI

struct HomeOwnView: View {
    var item: HomeTableItem
    @Binding var curDate : Date
    @StateObject var viewModel : HomeOwnViewModel
    
    init(item: HomeTableItem, curDate: Binding<Date>) {
        self.item = item
        self._curDate = curDate
        self._viewModel = StateObject(wrappedValue: HomeOwnViewModel(curDate: curDate, item: item))
    }
    
    var body: some View {
        ZStack{
            if !viewModel.status.isEmpty {
                HomeEmptyView(text1: viewModel.status,text2: viewModel.status2)
            } else {
                ScrollView {
                    //需要嵌套一层LazyVStack，不然会产生间距，而且大小还会随机
                    LazyVStack(spacing: 10) {
                        ForEach(viewModel.items,id: \.objectId) { item in
                            DiaryItemView(bean: item)
                        }
                        LoadMoreFooter(refreshing: $viewModel.footerRefreshing, action: {
                            viewModel.getData(false, item: item)
                        }) {
                            LoadMoreView(noMore: viewModel.noMore, footerRefreshing: viewModel.footerRefreshing)
                        }
                        .noMore(viewModel.noMore)
                        .preload(offset: 10)
                    }
                }
                .enableRefresh()
            }
            HStack{
                Spacer()
                VStack{
                    Spacer()
                    Image(.homeIconWrite)
                        .resizable()
                        .renderingMode(.template)
                        .frame(width: 25, height: 25)
                        .padding(8)
                        .foregroundColor(themeManager.themeColor)
                        .background(themeManager.themeColor.opacity(0.15))
                        .clipShape(Circle())
                        .contentShape(Rectangle())
                        .onTapGesture {
                            if AppStorageManager.shared.dateMode == IDateModeType.DAY {
                                let bean = DiaryBean(content: "")
                                bean.time = getMillisecond(curDate.timeIntervalSince1970)
                                pathManager.jump(type: JumpType.ADD_DIARY,data: bean)
                            } else {
                                pathManager.jump(type: JumpType.ADD_DIARY)
                            }
                        }
                }.padding(.bottom,30)
                    .padding(.trailing,20)
            }.background(.clear)
        }
        .onChange(of: AppStorageManager.shared.dateMode) {_ in
            if viewModel.isInit {
                viewModel.items.removeAll()
                viewModel.getData(true, item : item)
            }
        }
        .onChange(of: curDate) {_ in
            if viewModel.isInit {
                viewModel.items.removeAll()
                viewModel.getData(true, item : item)
            }
        }
        .onAppear{
            if !viewModel.isInit {
                viewModel.getData(true,item : item)
                viewModel.isInit = true
            }
        }
    }
}

#Preview {
    HomeOwnView(item: HomeTableItem(id: "", name: "ai日记"),curDate: .constant(Date()))
}
